<template>
  <div class="app-container">
    <el-row :gutter="10">
      <!--树结构-->
      <el-col :span="isHide ? 1 : 5" :xs="24" class="relative">
        <div class="app-container-tree-border">
          <project-tree-select-inner class="cd-mb8" :projectId="projectId" @changeProject="changeProject" />
          <el-tree :data="treeList" :props="defaultProps" :expand-on-click-node="false" ref="tree" node-key="id"
            highlight-current @node-click="handleNodeClick">
            <div slot-scope="{node, data}" class="custom-tree-node" :title="node.label" v-text="node.label"></div>
          </el-tree>
          <div class="tree-hide-button iconfont" :class="isHide ? 'icon-xiangyou' : 'icon-xiangzuo'"
            @click="() => { isHide = !isHide }">
          </div>
        </div>
      </el-col>
      <el-col :span="isHide ? 23 : 18" :xs="24">

        <div class="app-container-table-border" v-if="treeInfo && treeInfo.type !== 'supplier'">
          <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
            label-width="92px">
            <el-form-item label="供货单位名称" prop="name">
              <el-select v-model="queryParams.deptId" placeholder="请选择单位" style="width: 100%;" filterable clearable
                @change="handleQuery()">
                <el-option v-for="item in deptOptions" :key="item.deptId" :label="item.deptName" :value="item.deptId" />
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
              <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
            <el-row :gutter="10" class="mb8 top-right-btn small-line-height">
              <el-col :span="1.5" v-show="queryParams.zdId">
                <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                  v-hasPermi="['ma:materialSupplier:add']">新增</el-button>
              </el-col>
            </el-row>
          </el-form>
          <el-table border stripe v-loading="loading" :data="dataList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="供货单位名称" align="center" prop="deptName" min-width="150" show-overflow-tooltip />
            <el-table-column label="所属站点" align="center" prop="zdName" min-width="150" show-overflow-tooltip />
            <el-table-column label="供货材料名称" align="center" prop="ghcl" min-width="120" show-overflow-tooltip />
            <el-table-column label="联系人" align="center" prop="contacts" min-width="100" show-overflow-tooltip />
            <el-table-column label="联系电话" align="center" prop="phone" min-width="120" show-overflow-tooltip />
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="120">
              <template slot-scope="scope">
                <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                  v-hasPermi="['ma:materialSupplier:edit']">修改</el-button>
                <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                  v-hasPermi="['ma:materialSupplier:remove']">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
          <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize" @pagination="getList" />
        </div>
        <div class="app-container-table-border" v-if="treeInfo && treeInfo.type == 'supplier'">
          <el-descriptions border title="" :column="1" size="medium" labelClassName="descriptions-label"
            contentClassName="descriptions-content">
            <el-descriptions-item label="项目信息：" span="1">
              {{ form.projectName }}
            </el-descriptions-item>
            <el-descriptions-item label="所属标段：" span="1">
              {{ form.bdName }}
            </el-descriptions-item>
            <el-descriptions-item label="所属站点：" span="1">
              {{ form.zdName }}
            </el-descriptions-item>
            <el-descriptions-item label="供货商名称：" span="1">
              {{ form.deptName }}
            </el-descriptions-item>
            <el-descriptions-item label="供货材料：" span="1">
              {{ form.ghcl }}
            </el-descriptions-item>
            <el-descriptions-item label="联系人：" span="1">
              {{ form.contacts }}
            </el-descriptions-item>
            <el-descriptions-item label="联系电话：" span="1">
              {{ form.phone }}
            </el-descriptions-item>
            <el-descriptions-item label="传真电话：" span="1">
              {{ form.fax }}
            </el-descriptions-item>
            <el-descriptions-item label="移动电话：" span="1">
              {{ form.mobile }}
            </el-descriptions-item>
            <el-descriptions-item label="地址：" span="1">
              {{ form.address }}
            </el-descriptions-item>
            <el-descriptions-item label="简介：" span="1">
              {{ form.introduction }}
            </el-descriptions-item>
            <el-descriptions-item label="备注：" span="1">
              {{ form.remark }}
            </el-descriptions-item>
            <el-descriptions-item label="附件：" span="1">
              <fileUpload v-model="form.fileId" :oldList="form.fileIdOld" :fileType="['doc', 'docx', 'xlsx', 'xls'
                , 'pdf', 'png', 'jpg', 'jpeg']" :disabled="true" />
            </el-descriptions-item>
          </el-descriptions>
        </div>
      </el-col>
    </el-row>
    <!-- 添加或修改供货商对话框 -->
    <jy-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="95px">
        <el-row :gutter="8">
          <el-col :span="12">
            <el-form-item label="供货商名称" prop="deptId">
              <el-select v-model="form.deptId" placeholder="请选择单位" style="width: 100%;" filterable
                @change="setDeptName()">
                <el-option v-for="item in deptOptions" :key="item.deptId" :label="item.deptName"
                  :value="item.deptId + ''" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="供货材料" prop="ghcl">
              <!-- <el-input v-model="form.ghcl" placeholder="请输入供货材料" /> -->
              <el-select v-model="form.ghclDictValue" placeholder="供货材料" style="width: 100%;" @change="ghclChange">
                <el-option v-for="dict in dict.type.material_manifest_type" :key="dict.value" :label="dict.label"
                  :value="dict.value" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="8">
          <el-col :span="12">
            <el-form-item label="联系人" prop="contacts">
              <el-input v-model="form.contacts" placeholder="请输入联系人" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系电话" prop="phone">
              <el-input v-model="form.phone" placeholder="请输入联系电话" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="8">
          <el-col :span="12">
            <el-form-item label="传真电话" prop="fax">
              <el-input v-model="form.fax" placeholder="请输入传真电话" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="移动电话" prop="mobile">
              <el-input v-model="form.mobile" placeholder="请输入移动电话" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="8">
          <el-col :span="24">
            <el-form-item label="地址" prop="address">
              <el-input v-model="form.address" placeholder="请输入地址" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="8">
          <el-col :span="24">
            <el-form-item label="简介" prop="introduction">
              <el-input v-model="form.introduction" placeholder="请输入简介" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="8">
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" placeholder="请输入备注" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="附件" prop="fileId">
          <fileUpload v-model="form.fileId" :oldList="form.fileIdOld" :fileType="['doc', 'docx', 'xlsx', 'xls'
            , 'pdf', 'png', 'jpg', 'jpeg']" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </jy-dialog>
  </div>
</template>

<script>
import { listDeptByType } from "@/api/system/dept";
import { querySupplierTreeList } from "@/api/base/bsBd";
import { listMaterialSupplier, getMaterialSupplier, delMaterialSupplier, addMaterialSupplier, updateMaterialSupplier } from "@/api/base/supplier";
import { mapGetters } from "vuex";
import decimal from "@/utils/big-decimal";

export default {
  name: "Supplier",
  dicts: ["material_manifest_type"],
  data () {
    return {
      isHide: false,
      treeList: [],
      treeInfo: {},
      defaultProps: {
        children: "children",
        label: "label"
      },
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 供货商表格数据
      dataList: [],
      deptOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        bdId: undefined,
        deptId: undefined,
        bdId: undefined,
        deptId: undefined,
      },
      projectId: undefined,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        deptId: [
          { required: true, message: "供应商名称不能为空", trigger: "blur" }
        ],
        ghclDictValue: [
          { required: true, message: "供货材料不能为空", trigger: "blur" }
        ],
        contacts: [
          { required: true, message: "联系人不能为空", trigger: "blur" }
        ],
        phone: [
          { required: true, message: "联系电话不能为空", trigger: "blur" }
        ],
        fax: [
          { required: true, message: "传真不能为空", trigger: "blur" }
        ],
        mobile: [
          { required: true, message: "移动电话不能为空", trigger: "blur" }
        ],
        // fileId: [
        //   { required: true, message: "附件id不能为空", trigger: "blur" }
        // ]
      }
    };
  },
  // 监听顶部全局下拉
  computed: {
    ...mapGetters(["sysProjectId", "sysProjectList"])
  },
  watch: {
    sysProjectId (newVal, oldVal) {
      // 系统顶部选中项目变化
      this.changeProject(newVal);
    }
  },
  created () {
    this.changeProject();
    // 获取供货商下拉
    this.getDeptList();
  },
  methods: {
    changeProject (projectId = this.sysProjectId) {
      this.projectId = projectId;
      this.projectChangeResetQuery();
      this.getBdTree();
    },
    getBdTree () {
      querySupplierTreeList({ projectId: this.projectId }).then((response) => {
        this.treeList = response.data;
        // 选中左侧树
        this.$nextTick(() => {
          if (this.treeList.length > 0) {
            let treeInfo = (this.treeInfo && this.treeInfo.id) ? this.treeInfo : this.treeList[0];
            this.$refs.tree.setCurrentKey(treeInfo.id);
            this.handleNodeClick(treeInfo);
          } else {
            this.treeInfo = {};
            this.queryParams.bdId = null;
            this.queryParams.zdId = null;
            this.handleQuery();
          }
        });
      });
    },
    handleNodeClick (data) {
      this.treeInfo = data;
      switch (data.type) {
        case "bd":
          this.queryParams.bdId = data.parentId; //标段ID
          this.queryParams.zdId = null; //站点ID
          this.handleQuery();
          break;
        case "zd":
          this.queryParams.bdId = data.parentId; //标段ID
          this.queryParams.zdId = data.id; //站点ID
          this.handleQuery();
          break;
        case "supplier":
          this.queryParams.bdId = data.parentId; // 站点ID
          this.getInfo(data.id);
          break;
      }
    },
    projectChangeResetQuery () {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        qsId: undefined,
        bdId: undefined,
      };
      this.total = 0;
      this.dataList = [];
    },
    /** 查询供货商列表 */
    getList () {
      this.loading = true;
      listMaterialSupplier(this.queryParams).then(response => {
        this.dataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 获取供货商下拉
    getDeptList () {
      const params = {
        deptType: 6
      };
      listDeptByType(params).then(response => {
        this.deptOptions = response.data;
      });
    },
    // 取消按钮
    cancel () {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset () {
      this.form = {
        supplierId: undefined,
        projectId: this.sysProjectId,
        bdId: this.treeInfo.parentId,
        zdId: this.treeInfo.id,
        deptId: undefined,
        deptName: undefined,
        name: undefined,
        ghcl: undefined,
        ghclDictValue: undefined,
        contacts: undefined,
        phone: undefined,
        fax: undefined,
        mobile: undefined,
        address: undefined,
        introduction: undefined,
        remark: undefined,
        fileId: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange (selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd () {
      this.reset();
      this.open = true;
      this.title = "添加供货商";
    },
    /** 修改按钮操作 */
    handleUpdate (row) {
      this.loading = true;
      this.reset();
      this.getInfo(row.supplierId);
      this.open = true;
      this.title = "修改供货商";
    },
    getInfo (supplierId) {
      this.form.supplierId = supplierId;
      getMaterialSupplier(this.form.supplierId).then(response => {
        this.loading = false;
        this.form = response.data;
        if (this.form.fileId) {
          this.form.fileIdOld = this.form.fileId.split(',');
        }
      });
    },
    setDeptName () {
      let info = this.deptOptions.find(item => item.deptId == this.form.deptId);
      this.form.deptName = info.deptName;
    },
    ghclChange () {
      const info = this.dict.type.material_manifest_type.find(item => item.value == this.form.ghclDictValue);
      this.form.ghcl = info.label;
    },
    /** 提交按钮 */
    submitForm () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.fileId) {
            this.form.fileIds = this.form.fileId.split(',');
          }
          if (this.form.supplierId != null) {
            updateMaterialSupplier(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addMaterialSupplier(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete (row) {
      const ids = row.supplierId || this.ids;
      this.$modal.confirm('是否确认删除供货商名称为"' + row.name + '"的数据项？').then(() => {
        this.loading = true;
        return delMaterialSupplier(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport () {
      this.download('base/bsZd/export', {
        ...this.queryParams
      }, `bsZd_${new Date().getTime()}.xlsx`);
    }
  }
};
</script>
<style lang="scss" scoped>
.input-number {
  ::v-deep .el-input__inner {
    text-align: left;
  }
}

::v-deep {
  .descriptions-label {
    width: 120px;
    text-align: right !important;
    padding: 5px 0 !important;
  }

  .descriptions-content {
    // padding: 5px !important;

    .el-form-item {
      margin-bottom: 0px !important;
    }
  }
}
</style>
